#include "Terminal.h"
#include "TerminalPosition.h"
#include "../Memory/Helpers.h"

namespace IO{
    Terminal::Terminal(){}
    void Terminal::ClearTerminal(){
        Memory::Helpers::SetMemory((uint32_t)terminalBuffer, 0, 4000); //4000 is the number of bytes in a 80x25 buffer
        SetPosition(TerminalPosition(0,0));
    }

    void Terminal::Put_Char(const char c){
        const int index = _ypos*80+_xpos;
        terminalBuffer[index] = (_color<<8) | c;
        _xpos++;
        if(_xpos>79){
            _ypos++;
            _xpos=0;
        }
    }

    void Terminal::Put_String(const char* str){
        while(*str!='\0'){
            if(*str=='\n'){
                _ypos++;
                _xpos=0;
            }else{
                Put_Char(*str);
            }
            str++;
        }
    }

    void Terminal::SetPosition(TerminalPosition pos){
        _xpos = pos.Get_xpos();
        _ypos = pos.Get_ypos();
    }

    void Terminal::SetForegroundColor(uint8_t color){
        _color=color&0x0F;
    }

    void Terminal::SetBackgroundColor(uint8_t color){
        _color=color<<4&0xF;
    }
    

    
}
